
var serviceUrl = $api.getStorage('serviceUrl'); //服务器地址

var advList=[];  //广告数据
var productList = [];  //商品数据


//页面加载
apiready = function(){
   getindexData();
 //  openScrollPicture();
 
 
  api.setRefreshHeaderInfo({
        visible: true,
        // loadingImgae: 'wgt://image/refresh-white.png',
        bgColor: '#f2f2f2',
        textColor: '#4d4d4d',
        textDown: '下拉刷新...',
        textUp: '松开刷新...',
        showTime: true
    }, function (ret, err) {
        getindexData();
        
        api.refreshHeaderLoadDone();
    });
 
};


//获取首页信息
function getindexData(){
   
   do_ajax('/app/indexpro','get',{},function(ret,err){
       
       if(ret){
           advList =ret.ad1;
           productList=ret.datas;
           
     //      alert(advList);
           
           openScrollPicture();    //打开滑动图片
           
         //  return;
           
            setTimeout(function () {
		        	productList = ret.datas;
		        	showProductList();       //显示商品数据
	        }, 500);
	    
	       
	    //   api.hideProgress();
	    
       }else{
            api.alert({
	            msg:('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode)
	        });
	        api.hideProgress();
       
       }
   
   
   });
   


};


//设置滑动图片
function openScrollPicture(){

    var paths = [];

   
    for(var i=0;i<advList.length;i++){
	     paths.push(serviceUrl+'/files/'+advList[i].img);
	}

	var obj = api.require('UIScrollPicture');
	
//	var $right = $api.byId('rightw');
//	var rightwidth=$api.offset($right).w;   //右边宽度
//	var picturewidth=api.winWidth-rightwidth;  
	
	
	obj.open({
	    rect: {
	        x: 0, y: 0, w:api.winWidth, h:195
	    },
	    data: {
	        paths: paths,
	    },
	    styles: {
	        indicator: {
	            align: 'center',
	            color: '#666',
	            activeColor: '#ffaa43'
	        }
	    },
	    placeholderImg: 'widget://res/slide1.jpg',
	    contentMode: 'scaleToFill',
	    interval: 3,
	    loop: true,
	    fixedOn: 'index',
	    fixed: false
	}, function(ret, err){
	     if(ret.status){
	        if(ret.eventType == 'click'){
	            //点击图片的操作
	            //alert(ret.index);
	            var tolist=advList[ret.index].tolist;
	            var proid=advList[ret.index].proid;
	            if(tolist=='true'){ 
	               //去列表页
	               if(proid!=''){
	                  
	                   openWin('productlist',{'parentId':proid});
	                
	               }
	            
	            }
	            if(tolist=='false'){ 
	               //去详情页
	              
	               if(proid!=''){
	                     do_ajax('/app/productshow?id='+proid,'get',{},function(ret,err){
                            api.hideProgress();
				            if(ret){
				                openWin('productinfo',{product: ret});
				                
				            }else{
				                api.alert({
						            msg:('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode)
						        });
						       
				            }
				            
				          });
	                    
	               }
	               
	               
	               
	            }
	            
	            
	            
	            
	        } //  end
	     }
	});

    
    
    
};



//显示商品数据
function showProductList(){
    	
        var pcontent = $api.byId('pcontent');
		
		for(var i=0;i<productList.length;i++){
		var hV = Math.ceil(productList[i].productlist.length/2) * 230;
		var aHtml = '';
			aHtml += '<div data-am-widget="titlebar" class="am-titlebar am-titlebar-default am-margin-zero am-titlebar-multi am-no-layout">';
						aHtml +='<h2 class="am-titlebar-title"><a href="#" class="">'+productList[i].name+'</a></h2>';
						aHtml +='<nav class="am-titlebar-nav" onclick="toList('+ i +')">';
							aHtml +='more »';
						aHtml +='</nav>';
					aHtml +='</div>';
					aHtml +='<div class="js-masonry" style="position: relative; windth:389px; height: '+hV+'px;z-index:30;">';
					
    		for(var j=0;j<productList[i].productlist.length;j++){
    			var left = api.winWidth/2-5;
    			var PdivLeft = Math.floor(j%2)===0?0:left;
			var PdivTop = Math.floor(j/2)* 230;
    			aHtml +='<div class="am-u-sm-6 goodsitem" style="position: absolute; left: '+PdivLeft+'px; top: '+PdivTop+'px;">';
							aHtml +='<div class="am-thumbnail">';
									aHtml +='<img  onclick="openProduct('+i+','+j+');"   src="'+serviceUrl+'/files/'+(productList[i].productlist[j].proDefImg||0)+'" class="scrollLoading"  style="width:200px;height:150px;" alt="" />';
								aHtml +='<p class="am-thumbnail-caption am-text-truncate am-text-xs">';
										aHtml +=productList[i].productlist[j].name
								aHtml +='</p>';
								
								aHtml +='<p class="am-thumbnail-caption index-cart am-text-secondary">';
									aHtml +='<s>￥'+(productList[i].productlist[j].price/(productList[i].productlist[j].bfb||0.8)).toFixed(2)+'</s>';
									
									if(productList[i].productlist[j].type!=='服务产品' ){
											aHtml +='<i onclick="addCratItem(\''+productList[i].productlist[j]._id +'\');" class="am-icon-shopping-cart am-text-primary" style="float: right;"></i>';
									}
								
									
								aHtml +='</p>';
								
							 aHtml +='<p class="am-thumbnail-caption index-cart am-text-primary" style="font-size:14px">';
								 aHtml +='<span class="am-text-xs">在线支付￥ '+productList[i].productlist[j].price.toFixed(2)+' </span>';
							aHtml +='</p>';
								
								
							aHtml +='</div>';
						aHtml +='</div>';
    			
	    	}
	    	aHtml+='</div>';
	    	$api.html(pcontent, aHtml);
    	}
    	api.hideProgress();


};

//商品详情
 //cindex 是商品类型数组下标 ，pindex 是商品数组下标。商品数组在商品类型数组中 productList[cindex].productlist[pindex]
function openProduct(cindex,pindex){

    	var productObj = productList[cindex].productlist[pindex];
    	              //窗口名     参数
    	openWin('productinfo',{product: productObj});
    	
//  	  api.openWin({
//	        name: 'addmsg',
//	        url: 'addmsg.html',
//	        showProgress:true,
//	         pageParam: {product: productObj},
//	        opaque: true,
//	        vScrollBarEnabled:false
//	    });


};


//商品列表
function toList(index){
//	vc.titleString = [dict objectForKey:@"name"];
//  vc.parentId = [dict objectForKey:@"_id"];
//  vc.childList = [dict objectForKey:@"childs"];
	var data = productList[index];
	
	var title = data.name;
	var parentId = data._id;
	var childList = data.childs;
	
	openWin('productlist',{'title':title, 'parentId':parentId, 'childList':childList});
}






